package com.examsystem.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fire'ge
 */
@Controller

public class CheckUserController {

    @Resource
    RedisTemplate<Object,Object> redisTemplate;

    Map<String,Integer> conditionMap= new HashMap<>();


    class RemoveUserOnLine implements Runnable
    {
        private final int userId;
        public RemoveUserOnLine(int userId) {
            this.userId = userId;
        }

        @Override
        public void run() {
            Collection<Integer> values = conditionMap.values();
            for (int userCode:values) {
                if (userCode<=0){
                    redisTemplate.delete("login:user"+userId);
                }
            }
        }
    }

    class DecrUserCode implements Runnable
    {
        private final int userId;
        public DecrUserCode(int userId) {
            this.userId = userId;
        }

        @Override
        public void run() {
            Collection<Integer> values = conditionMap.values();
            for (int userCode:values) {
                conditionMap.put("userCode:"+userId,--userCode);
            }
        }
    }
}
